﻿Imports Microsoft.VisualBasic
Imports System.ComponentModel
Imports System.Data
Imports System.Data.Common
<System.ComponentModel.DataObject()> _
Public Class Terceros
    Inherits BDDatos
    Private _CodAux As String
    Property CodAux() As String
        Get
            Return _CodAux
        End Get
        Set(ByVal value As String)
            _CodAux = value
        End Set
    End Property
    Sub New()
        Me.Tabla = "Terceros"
        Me.Vista = "Vterceros"
        Me.VistaDB = "Vterceros"
    End Sub
    <DataObjectMethodAttribute(DataObjectMethodType.Select, True)> _
    Public Overloads Function GetRecords(ByVal busc As String) As DataTable
        Me.Conectar()
        If busc = "" Then
            querystring = "SELECT * FROM Vterceros"
            Me.CrearComando(querystring)
        Else
            querystring = "SELECT * FROM Vterceros where Ced_Tercero Like :Buscar or Nom_Completo Like :Buscar"
            Me.CrearComando(querystring)
            AsignarParametroCadena(":Buscar", "%" & busc & "%")
            AsignarParametroCadena(":Buscar", "%" & busc & "%")
        End If
        Dim dataTb As DataTable = Me.EjecutarConsultaDataTable()
        Me.Desconectar()
        Return dataTb
    End Function
    <DataObjectMethodAttribute(DataObjectMethodType.Select, True)> _
    Public Overloads Function GetRecords(ByVal busc As String, ByVal tipo As String) As DataTable
        Me.Conectar()
        '++++++++++ TIPO: 
        '** AR -> AGENTE RECAUDADOR
        '** RT -> RENTAS
        '** OT -> OTRO - PD o P
        '**
        Dim tp As String = "OT"
        If (tipo = "AR") Or (tipo = "RT") Then
            tp = tipo
        End If
        Dim queryString As String
        If busc <> "" Then
            busc = "%" + UCase(busc) + "%"
            queryString = "SELECT * FROM VTERCEROS WHERE (Ced_Tercero like :busc) OR (upper(Nom_Completo) like :busc) "
            Me.CrearComando(queryString)
            Me.AsignarParametroCadena(":busc", busc)
            Me.AsignarParametroCadena(":busc", busc)
        Else
            queryString = "SELECT * FROM VTERCEROS WHERE 1<>1"
            Me.CrearComando(queryString)
        End If
        Dim dataTb As DataTable = Me.EjecutarConsultaDataTable()
        Me.Desconectar()
        Return dataTb
    End Function
    <DataObjectMethodAttribute(DataObjectMethodType.Select, True)> _
    Public Overloads Function GetByUser(Optional ByVal User As String = "") As DataTable
        'Si no recibe ningún Usuario, buscará los Datos del Usuario Conectado
        If User = "" Then
            User = Me.usuario
        End If
        'Me.Desconectar()
        Me.Conectar()
        querystring = "SELECT * FROM vTerceros WHERE Ced_Tercero=:Ide_Ter"
        Me.CrearComando(querystring)
        Me.AsignarParametroCadena(":Ide_Ter", User)
        Dim dataTb As DataTable = Me.EjecutarConsultaDataTable()
        Me.Desconectar()
        Return dataTb

    End Function
    Public Function Insert(ByVal tipodoc As String, ByVal id As String, ByVal expe As String, ByVal nom_ter As String, ByVal ape_ter As String, ByVal tel_ter As String, ByVal dir_ter As String, ByVal dep As String, ByVal ciu As String, ByVal ema_ter As String) As String
        Me.Conectar()
        Me.ComenzarTransaccion()
        Try
            If tipodoc <> "CA" Then
                Me.querystring = "Insert into Terceros(Tipo_doc,Ced_Tercero,Lugar_Exp,Nom_Tercero,Ape_Tercero,Telefono,Direccion,Departa,Ciudad,Email) Values(:tipod,:Ced_Tercero,:LugarEx,:Nom_Tercero,:Ape_Tercero,:Telefono,:Direccion,:Depar,:Ciud,:Email)"
                Me.CrearComando(querystring)
                Me.AsignarParametroCadena(":tipod", tipodoc)
                Me.AsignarParametroCadena(":Ced_Tercero", id)
                Me.AsignarParametroCadena(":LugarEx", expe)
                Me.AsignarParametroCadena(":Nom_Tercero", UCase(nom_ter))
                Me.AsignarParametroCadena(":Ape_Tercero", UCase(ape_ter))
                Me.AsignarParametroCadena(":Telefono", tel_ter)
                Me.AsignarParametroCadena(":Direccion", UCase(dir_ter))
                Me.AsignarParametroCadena(":Depar", dep)
                Me.AsignarParametroCadena(":Ciud", ciu)
                Me.AsignarParametroCadena(":Email", UCase(ema_ter))
            Else
                CrearCodAux()
                Me.querystring = "Insert into Terceros(Tipo_doc,Ced_Tercero,Lugar_Exp,Nom_Tercero,Ape_Tercero,Telefono,Direccion,Departa,Ciudad,Email) Values(:tipod,:Ced_Tercero,:LugarEx,:Nom_Tercero,:Ape_Tercero,:Telefono,:Direccion,:Depar,:Ciud,:Email)"
                Me.CrearComando(querystring)
                Me.AsignarParametroCadena(":tipod", tipodoc)
                Me.AsignarParametroCadena(":Ced_Tercero", CodAux)
                Me.AsignarParametroCadena(":LugarEx", expe)
                Me.AsignarParametroCadena(":Nom_Tercero", UCase(nom_ter))
                Me.AsignarParametroCadena(":Ape_Tercero", UCase(ape_ter))
                Me.AsignarParametroCadena(":Telefono", tel_ter)
                Me.AsignarParametroCadena(":Direccion", UCase(dir_ter))
                Me.AsignarParametroCadena(":Depar", dep)
                Me.AsignarParametroCadena(":Ciud", ciu)
                Me.AsignarParametroCadena(":Email", UCase(ema_ter))
            End If
            Me.num_reg = EjecutarComando()
            Me.Msg = "Se realizo la Operacion Exitosamente - " + num_reg.ToString + " Filas Afectadas"
            Me.lErrorG = False
            Me.ConfirmarTransaccion()
        Catch ex As Exception
            CancelarTransaccion()
            Me.lErrorG = True
            Me.Msg = "Err: " + ex.Message
        Finally
            Me.Desconectar()
        End Try
        Return Msg
    End Function
    Public Function Update(ByVal PK As String, ByVal Tipo_Doc As String, ByVal id As String, ByVal expo As String, ByVal nom_ter As String, ByVal ape_ter As String, ByVal tel_ter As String, ByVal dir_ter As String, ByVal dep As String, ByVal ciu As String, ByVal ema_ter As String) As String
        Me.Conectar()
        Me.ComenzarTransaccion()
        Try
            Me.querystring = "Update Terceros set Tipo_Doc=:Tipo_Doc, Ced_Tercero=:Ced_Tercero,Lugar_Exp=:Exped,Nom_Tercero=:Nom_Tercero, Ape_Tercero=:Ape_Tercero, Telefono=:Telefono, Direccion=:Direccion, Departa=:Depa, Ciudad=:Ciud, Email=:Email Where Ced_Tercero=:PK"
            Me.CrearComando(querystring)
            Me.AsignarParametroCadena(":Tipo_Doc", Tipo_Doc)
            Me.AsignarParametroCadena(":Ced_Tercero", id)
            Me.AsignarParametroCadena(":Exped", UCase(expo))
            Me.AsignarParametroCadena(":Nom_Tercero", UCase(nom_ter))
            Me.AsignarParametroCadena(":Ape_Tercero", UCase(ape_ter))
            Me.AsignarParametroCadena(":Telefono", tel_ter)
            Me.AsignarParametroCadena(":Direccion", UCase(dir_ter))
            Me.AsignarParametroCadena(":Depa", dep)
            Me.AsignarParametroCadena(":Ciud", ciu)
            Me.AsignarParametroCadena(":Email", ema_ter)
            Me.AsignarParametroCadena(":PK", PK)
            Me.num_reg = EjecutarComando()
            Me.Msg = "Se realizo la Operacion Exitosamente - " + num_reg.ToString + " Filas Afectadas"
            Me.lErrorG = False
            Me.ConfirmarTransaccion()
        Catch ex As Exception
            CancelarTransaccion()
            Me.lErrorG = True
            Me.Msg = "Err: " + ex.Message
        Finally
            Me.Desconectar()
        End Try
        Return Msg
    End Function
    Public Function GetByPK(ByVal idTer As String) As datatable
        Me.Conectar()
        Me.querystring = "Select * from vterceros where ced_tercero=:ced_tercero"
        CrearComando(querystring)
        Me.AsignarParametroCadena(":ced_tercero", idTer)
        Dim dt As DataTable = EjecutarConsultaDataTable()
        Me.Desconectar()
        Return dt
    End Function
    Public Function Delete(ByVal idTer As String) As String
        Me.Conectar()
        ComenzarTransaccion()
        Try
            Me.querystring = "Delete from terceros where ced_tercero=:ced_tercero"
            CrearComando(querystring)
            Me.AsignarParametroCadena(":ced_tercero", idTer)
            EjecutarComando()
            Msg = MsgOk
            ConfirmarTransaccion()
        Catch ex As Exception
            CancelarTransaccion()
            Msg = ex.Message
        Finally
            Me.Desconectar()
        End Try
        Return Msg
    End Function
    Private Sub CrearCodAux()
        querystring = "CodAux"
        CrearComando(querystring, CommandType.StoredProcedure)
        Dim preturn As DbParameter = AsignarParametroReturn(20)
        EjecutarComando()
        Me.CodAux = preturn.Value.ToString()
    End Sub
End Class
